<cfinclude template="/admin/application.cfm">

<cfparam name="formName" default="">

<cfquery name="getFormData" datasource="#datasourcename#">
	select * from form_data
	where fod_formname = '#formName#'
</cfquery>

<cfif getFormData.RecordCount gt 0>
	<cfsetting enablecfoutputonly="Yes" showdebugoutput="No">
	<cfcontent type="text/x-csv">
	<cfheader name="content-disposition" value="attachment; filename=export-#dateformat(now(), "yyyymmdd")##timeformat(now(), "hhmmss")#.csv">
	<cfset shownColumns = false>
	<cfloop query="getFormData">
		
		<cfif isWddx(fod_data)>
			<cfwddx action="WDDX2CFML" input="#fod_data#" output="data">
			
			<cfif structCount(data)>
				<cfif not shownColumns>
					<cfset fieldnameList = "">
					<cfloop index="i" list="#data.fieldnames#">
						<cfif listFirst(i, "_") neq "cfg">
							<cfset fieldnameList = listAppend(fieldnameList, """" & i & """")>
						</cfif>
					</cfloop>
					<cfoutput>"DATE",#fieldnameList##chr(13)#</cfoutput>
					<cfset shownColumns = true>
				</cfif>
				
				<cfset fieldValues = "">
				<cfloop index="i" list="#data.fieldnames#">
					<cfif listFirst(i, "_") neq "cfg">
						<cfset fieldValues = listappend(fieldValues, """" & replacenocase(replacenocase(htmleditformat(data[i]), chr(10), "", "ALL"), chr(13), "", "ALL") & """")>
					</cfif>
				</cfloop>
				<cfoutput>"#dateformat(fod_datesubmitted, "mm/dd/yyyy")# #timeformat(fod_datesubmitted, "h:mm:ss tt")#",#fieldValues##chr(13)#</cfoutput>
			</cfif>
		</cfif>
	
	</cfloop>
<cfelse>
	Nothing to Export.
</cfif>
